package com.addplus.townmall.server.scheduled.scheduletask;

import com.addplus.townmall.server.api.mapper.town.*;
import com.addplus.townmall.server.api.model.town.*;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.*;
import java.util.Date;
import java.util.List;

/**
 * 类名: MemberCartOnlineTask
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/11/8 2:57 PM
 * @description 类描述: 秒杀活动状态更改定时器
 */
@Component
public class MemberCartOnlineTask {

    private Logger logger = LoggerFactory.getLogger(MemberCartOnlineTask.class);

    @Autowired
    private DiscountCardMemberMapper discountCardMemberMapper;

    @Autowired
    CouponMapper couponMapper;


    @Autowired
    OrderItemsFightMapper orderItemsFightMapper;

    @Autowired
    TownOrderFightMapper townOrderFightMapper;
    @Autowired
    MemberIntegralMapper memberIntegralMapper;


    /**
     * 会员卡上架超时任务
     */
    @Scheduled(cron = "0 0 12 * * ?")
    private void setMemberCardNotOnline() throws Exception {
        logger.info("开始执行会员卡上架超时任务");
        Date date = new Date();
        List<DiscountCardMember> onlineCard = discountCardMemberMapper.selectList(new QueryWrapper<DiscountCardMember>()
                .eq("is_deleted", 0));
        onlineCard.stream().forEach(o -> {
            if (o.getEndDate() != null) {
                LocalDate onlineTime = o.getEndDate();
                if (!DataUtils.isEmptyObject(onlineTime)) {
                    if (onlineTime.isBefore(LocalDate.now())) { // 结束时间大于当前时间
                        o.setDiscuountStatus(2);// 设置为下架状态
                        discountCardMemberMapper.updateById(o);
                    }
                }
            }

            if (o.getStartDate() != null) {
                LocalDate onlineTime = o.getStartDate();
                if (!DataUtils.isEmptyObject(onlineTime)) {
                    if (onlineTime.isAfter(LocalDate.now())) { // 开始时间小于当前时间
                        o.setDiscuountStatus(1);// 设置为下架状态
                        discountCardMemberMapper.updateById(o);
                    }
                }
            }

        });
        logger.info("会员卡上架超时任务结束");
    }

}
